
cc.Class({
    extends: require('UI-PanelWindow'),

    properties: {
        videoCount :cc.Node,
        shareCount : cc.Node,
        videoGoldLabel :cc.Label,
        shareGoldLabel :cc.Label,
        lastVideoLabel :cc.Label,
        lastShareLabel : cc.Label,
        videoOrShare:[cc.SpriteFrame],
        videoGift:cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._super()
        this.nowDate=new Date()
        this.ymd=this.nowDate.getFullYear()+'-'+this.nowDate.getMonth()+'-'+this.nowDate.getDate();//当前年月日 2018-11-14
        this.updateHintButton()
        global.BannerAD.createBanner('adunit-4505ca0d79fae8df');
        // if(global.Data.videoCount==3){
        //     cc.find('view/revert/gift/zhaohuan',this.node).active=true
        // }
        // this.videoGift=cc.find('view/revert/sharebtn',this.node).getComponent(cc.Sprite);
        /*var rc = require('RewardComponent');
        this.has = rc.hasVideo();//是否能看视频
        if(this.has){
            this.videoGift.children[0].active=false
            this.videoGift.children[1].active=true
        }else{
            this.videoGift.children[0].active=false
            this.videoGift.children[1].active=true
        }*/
    },

    onDestroy(){
        global.BannerAD.destroy();
    },
    updateHintButton(){
        this.coinGet=Math.ceil(200*(global.Data.mainArmsLevel+1)*Math.pow(1.2,global.Data.mainArmsLevel))+Math.ceil(200*(global.Data.mainArmsLevel+2)*Math.pow(1.2,global.Data.mainArmsLevel+1))+Math.ceil(200*(global.Data.mainArmsLevel+3)*Math.pow(1.2,global.Data.mainArmsLevel+2));
        this.videoCount.active = global.Data.videoCount
        if(this.videoCount.active){
            this.videoCount.getComponentInChildren(cc.Label).string = global.Data.videoCount
        }
        this.shareCount.active = global.Data.shareCount
        if(this.shareCount.active){
            this.shareCount.getComponentInChildren(cc.Label).string = global.Data.shareCount
        }
        this.shareNumber=global.Data.dailyGifts.shareCount-global.Data.dailyGifts.receiveShareCount;
        this.videoNumber=global.Data.dailyGifts.videoCount-global.Data.dailyGifts.receiveVideoCount
        this.lastShareLabel.string = '剩余 '+(this.shareNumber)+'次'
        this.lastVideoLabel.string = '剩余 '+(this.videoNumber)+'次'
        this.videoGoldLabel.string = global.Common.getKV(this.coinGet);
        this.shareGoldLabel.string = global.Common.getKV(global.Data.dailyGifts.config.shareGoldCoin);

        cc.find('view/revert/video',this.node).getComponent(cc.Sprite).setState(this.videoNumber==0?1:0)
        cc.find('view/revert/share',this.node).getComponent(cc.Sprite).setState(this.shareNumber==0?1:0)

        // cc.find('view/revert/sharebtn/1',this.node).getComponent(cc.Button).interactable = this.videoNumber>0;
        // cc.find('view/revert/sharebtn/2',this.node).getComponent(cc.Button).interactable = this.videoNumber>0;
        cc.find('view/revert/sharebtn1',this.node).getComponent(cc.Button).interactable = this.shareNumber>0;
    },

    start () {
        
    },
    clickCancel(){
        global.BannerAD.destroy();
        SoundMgr.playButtonEffect()
        global.EJMgr.popUI();
        if(global.gaming){
            cc.audioEngine.resumeAll();
            var pause = global.UIMgr.pushUI('game/GamePause')
            pause.getComponent('GameUI-Pause').setOptions({revert:true,cb:function(){}});
        }
    },
    clickVideo(data){
        SoundMgr.playButtonEffect()
        if(!data){
            global.EJMgr.showToast('获取金币失败')
            return;
        }
        var self=this

        var options={
            adUnitId:'adunit-6a07e5b943279784',
            stopCb:function(){
                global.EJMgr.showToast('视频未看完，不能获得奖励')
            },
            successCb:function(){
                global.Data.goldCoin += self.coinGet;
                global.Data.dailyGifts.receiveVideoCount ++;
                self.httpGet('/game/gameUser','attrAdd',{propName: "goldCoin",count: self.coinGet},function(err,res){
                    if(err){
                        global.Data.goldCoin -= self.coinGet;
                        global.Data.dailyGifts.receiveVideoCount--;
                        return;
                    }
                    self.updateHintButton();
                    global.EJMgr.showToast('获取金币成功');
                    self.clickCancel();
                })
            }
        }
        cc.log('广告按钮被点击');
        global.WechatShare.videoOrShare(options)
    },
    /*clickShare(){
        var self = this;

            if (!global.Data.isCheck) {
                var options={
                    successCb:function(){
                        cc.log('分享成功')
                        self.httpGet('/game/gameUser','dailyGifts.receiveShareGoldCoin',{},function(err,res){
                            if(err){
                                return;
                            }
                            global.Data.dailyGifts = res
                            global.Data.goldCoin += global.Data.dailyGifts.config.shareGoldCoin
                            self.updateHintButton();
                            global.EJMgr.showToast('获取金币成功')
                            self.clickCancel()
                        })
                    }
                }
                global.WechatShare.share(options);
            }else{
                self.httpGet('/game/gameUser','dailyGifts.receiveShareGoldCoin',{},function(err,res){
                    if(err){
                        return;
                    }
                    global.Data.dailyGifts = res
                    global.Data.goldCoin += global.Data.dailyGifts.config.shareGoldCoin
                    self.updateHintButton();
                    global.EJMgr.showToast('获取金币成功')
                    self.clickCancel()
                })
            }
            
        // }else{
        //     // if (!global.Data.isCheck) {
        //     //     global.WechatShare.share('还记得记忆中的雷电吗，快来跟我一起爽快射击吧',{parentId:global.Data._id},function(){
        //     //         self.httpGet('/game/gameUser','dailyGifts.receiveShareGoldCoin',{},function(err,res){
        //     //         })
        //     //     })
        //     // }
        //     global.EJMgr.showToast('获取次数不足')
        // }
    }*/

    // update (dt) {},
});
